<?php

include ("../model/kernel/common.php");
include ("../model/classes/Ubb/Ubb.class.php");

$ptn = "([^\[\"']+?)";

$urlpattern = "(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/)".$ptn;

$urlpattern2 = "=www.".$ptn;

$pattern = "/";
$pattern .= "\[(url)$urlpattern2\]([^\[\"']*?)\[\/url]|";
$pattern .= "\[(url)]$urlpattern\[\/url]|";
$pattern .= "\[(url)=$urlpattern]$ptn\[\/url]";
$pattern .= "/is";


$ubbtest = "[url]http://www.baidu.com[/url][url=www.baidu.com][/url][url=www.baidu.com]test[/url]";

function array_trim($a)
{
	if (is_array($a))
	{
		$_T = array();
		foreach ($a as $K => $V) {
			!empty($V) && $_T[] = $V;
		}
		return $_T;
	}
	return false;
}

function a($_m)
{
	$_m = array_trim($_m);
	if(is_array($_m)) {
		switch(strtolower($_m[1]))
		{
			case "url" :
				return parseUrl($_m);
		}
	}
}

function parseUrl($_url) {

	$hyperlink = !Validate::_isValidURL($_url[2]) ? cutUrl($_url[3]) : "<a href=\"http://www.{$_url[2]}\" target=\"_blank\" class=\"bbcode_url\">{$_url[2]}</a>";

	if (!empty($_url[4])) {
		$hyperlink = "<a href=\"{$_url[2]}://{$_url[3]}\" target=\"_blank\" class=\"bbcode_url\">{$_url[4]}</a>";
	}
	
	return $hyperlink;
}

function cutUrl($url) {
	$length = 65;
	$urllink = "<a href=\"".(strncasecmp($url,"www.",4) == 0 ? "http://$url" : $url).'" target="_blank" class="bbcode_url">';
	if(strlen($url) > $length) {
		$url = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
	}
	$urllink .= $url.'</a>';
	return $urllink;
}

$i = 0;
$startTime = sprintf('%.8f',array_sum(explode(' ', microtime())));
while(1)
{
	preg_replace_callback($pattern,'a',$ubbtest);
	
	$i++;
	if ($i == 5000)
		break;
}

$endTime = sprintf('%0.6f',sprintf('%.8f',array_sum(explode(' ', microtime()))) - $startTime);
echo $endTime."<br />";




/////////////////////////////////////////////
$u = new Ubb();
$startTime = sprintf('%.8f',array_sum(explode(' ', microtime())));
$i = 0;

while(1)
{
	$u -> parseUbb($ubbtest);
	$i++;
	if ($i == 5000)
		break;
}

$endTime = sprintf('%0.6f',sprintf('%.8f',array_sum(explode(' ', microtime()))) - $startTime);
echo $endTime."<br />";
?>
